<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<style type="text/css">
    .hide { display:none }
    td:nth-child(4) {
        text-align: center;
    }
    .layui-input:focus {
        border-color: #009688 !important;
    }
</style>
<body>
<div class="layui-fluid">
    <fieldset class="layui-elem-field">
        <legend>内容分类</legend>
        <div class="layui-field-box">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <button class="layui-btn add">添加</button>
                    <button class="layui-btn layui-btn-primary up-all">全部收起</button>
                    <button class="layui-btn layui-btn-primary down-all">全部展开</button>
                </div>
                <div class="layui-card-body">
                    <table class="layui-table layui-form" id="test-tree-table"></table>
                </div>
            </div>
        </div>
    </fieldset>
</div>

<script type="text/javascript" src="/static/layuiadmin/layui/layui.js"></script>
<script type="text/javascript">
    layui.config({
        base: '/static/js/'
    });
    layui.use(['treetable','form'],function(){
        var data = {$list};
        var $ = layui.$,treetable = layui.treetable;
        var form = layui.form,layer = layui.layer;
        treetable.render({
            elem: '#test-tree-table',
            data: data,
            field: 'name',
            cols: [
                {
                    field: 'sort',
                    title: '排序',
                    width: '5%',
                    template: function(item){
                        return '<input type="text" placeholder="排序" autocomplete="off" class="layui-input sort" data-id="'+item.id+'" value="'+item.sort+'" style="height: 28px;">';
                    },
                    edit: 'text'
                },
                {
                    field: 'name',
                    title: '分类名称',
                    width: '45%',
                    template: function(item){
                        if(item.level == 1){
                            return '<span style="color:red;">'+item.name+'</span>';
                        }
                        if(item.level == 2){
                            return '<span style="color:green;">'+item.name+'</span>';
                        }
                        return item.name;
                    }
                },
                {
                    field: 'status',
                    title: '状态',
                    width: '25%',
                    template: function(item){
                        if (item.status == 0){
                            return '<input type="checkbox" data-id="'+item.id+'" value="1" lay-skin="switch" lay-filter="status" lay-text="显示|隐藏">';
                        }else{
                            return '<input type="checkbox" data-id="'+item.id+'" value="1" lay-skin="switch" lay-filter="status" lay-text="显示|隐藏" checked>';
                        }
                    }
                },
                {
                    field: 'actions',
                    title: '操作',
                    width: '25%',
                    template: function(item){
                        var tem = [];
                        if(item.pid == 0){

                            tem.push('<a class="layui-btn layui-btn-normal layui-btn-xs" lay-filter="add"><i class="layui-icon layui-icon-add-1"></i>添加子集</a>');
                        }
                        tem.push('<a class="layui-btn layui-btn-normal layui-btn-xs" lay-filter="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>');
                        tem.push('<a class="layui-btn layui-btn-danger layui-btn-xs" lay-filter="del"><i class="layui-icon layui-icon-delete"></i>删除</a>');
                        return tem.join('')
                    }
                }
            ]
        });

        $('.layui-btn.add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加分类'
                ,content: "{:url('Help/help_category_operate')}"
                ,offset: ['20%', '32%']
                ,area: ['400px', '300px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        });

        treetable.on('treetable(edit)',function(data){
            var id = data.item.id;
            layer.open({
                type: 2
                ,title: '编辑分类'
                ,content: '/admin/Help/help_category_operate/id/' + id
                ,offset: ['20%', '32%']
                ,area: ['400px', '300px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");
                    submit.click();
                }
            });
        });

        treetable.on('treetable(add)',function(data){
            var id = data.item.id;
            layer.open({
                type: 2
                ,title: '添加分类'
                ,content: '/admin/Help/help_category_operate/up/' + id
                ,offset: ['20%', '32%']
                ,area: ['400px', '350px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                    //点击确认触发 iframe 内容中的按钮提交
                    var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                    submit.click();
                }
            });
        });

        treetable.on('treetable(del)',function(data){
            var id = data.item.id;
            layer.confirm('确定删除吗?', {
                icon: 3,
                title:'提示',
                offset: ['25%', '35%']
            }, function(){
                $.post("{:url('Help/help_category_del')}", {id:id}, function(e){
                    if (e.info == 1){
                        layer.msg('删除成功', {offset: ['30%', '40%']});
                        setTimeout(function (){
                            location.reload();
                        }, 500);
                    }else{
                        layer.msg(e.msg, {offset: ['30%', '40%']});
                    }
                }, 'json');
            });
        });

        form.on('switch(status)',function(){
            var index = layer.load(1, {offset: ['30%', '42%']});
            var id = $(this).data('id');
            var status = 0;
            if ($(this).is(':checked')){
                status = 1;
            }else{
                status = 0;
            }
            $.post("{:url('Help/status_edit')}", {id:id,status:status}, function(e){
                layer.close(index);
                if (e == 1){
                    layer.msg('修改成功', {offset: ['30%', '40%']});
                }else{
                    layer.msg('修改失败', {offset: ['30%', '40%']});
                }
            });
        });

        $(".sort").blur(function(){
            var index = layer.load(1, {offset: ['30%', '42%']});
            var id = $(this).data('id');
            var value = $(this).val();
            if (value === ''){
                layer.msg('排序不能为空');
                layer.close(index);
                return false;
            }else if (isNaN(value)){
                layer.msg('只能为数字');
                layer.close(index);
                return false;
            }
            $.post("{:url('Help/sort_edit')}", {id:id,value:value}, function(e){
                layer.close(index);
                if (e == 1){
                    layer.msg('修改成功', {offset: ['30%', '40%']});
                    setTimeout(function (){
                        location.reload();
                    }, 500);
                }else{
                    layer.msg('修改失败', {offset: ['30%', '40%']});
                }
            });
        });

        $('.up-all').click(function(){
            treetable.all('up');
        });

        $('.down-all').click(function(){
            treetable.all('down');
        });
    })
</script>
</body>
</html>
